<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: MetaSEO.php 425 2011-01-05 10:30:30Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalConfigRecord');
class MetaSEO extends PortalAdminPage
{
	const CONFIG_FILE = 'Application.Data.Settings';

	private $_metaseo	= null;

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->setAdminPageTitle('Meta / SEO');
		$this->setAdminPageTitle('Tracking Configuration');

		if(!$this->IsPostBack)
		{
			$this->cbFriendlyUrl->Checked	= TPropertyValue::ensureBoolean($this->Application->Parameters['FriendlyUrl']);
			$this->txtUrlPrefix->Text		= TPropertyValue::ensureString($this->Application->Parameters['UrlPrefix']);
		}

	}

	public function getMetaSEO()
	{
		if($this->_metaseo===null)
		{
			$this->_metaseo = new stdClass();
			$pcr = PortalConfigRecord::finder()->findByname('metaseo_metakeywords');

			if($pcr)
			{
				$this->_metaseo->metakeywords = $pcr->value;
			}
			else
			{
				$this->_metaseo->metakeywords	= '';
			}

			$pcr = PortalConfigRecord::finder()->findByname('metaseo_metadescription');

			if($pcr)
			{
				$this->_metaseo->metadescription = $pcr->value;
			}
			else
			{
				$this->_metaseo->metadescription	= '';
			}
		}

		return $this->_metaseo;
	}

	public function SaveMetaSeo($sender, $param)
	{
		if($this->IsValid)
		{
			$pcr = PortalConfigRecord::finder()->findByname('metaseo_metakeywords');
			if(!$pcr)
			{
				$pcr		= new PortalConfigRecord();
				$pcr->name	= 'metaseo_metakeywords';
			}

			$pcr->value	= $this->tbMetaKeywords->Text;
			$pcr->save();
			unset($pcr);

			$pcr = PortalConfigRecord::finder()->findByname('metaseo_metadescription');

			if(!$pcr)
			{
				$pcr		= new PortalConfigRecord();
				$pcr->name	= 'metaseo_metadescription';
			}

			$pcr->value	= $this->tbMetaDescription->Text;
			$pcr->save();

			// Save

			$dom = new TXmlDocument;
			if($dom->loadFromFile(Prado::getPathOfNamespace(self::CONFIG_FILE, '.xml')))
			{
				if(!$this->UpdateParameter($dom,'FriendlyUrl',$this->cbFriendlyUrl->Checked))
				{
					// Create paramtere
					$elements = $dom->Elements;
					$elements[]	= $this->createParameter('FriendlyUrl', $this->cbFriendlyUrl->Checked);
				}

				if(!$this->UpdateParameter($dom,'UrlPrefix',$this->txtUrlPrefix->Text))
				{
					// Create paramtere
					$elements = $dom->Elements;
					$elements[]	= $this->createParameter('UrlPrefix', $this->txtUrlPrefix->Text);
				}

				$dom->saveToFile(Prado::getPathOfNamespace(self::CONFIG_FILE, '.xml'));
			}
		}

		$this->gotoPage('Home');
	}

	public function UpdateParameter(&$dom,$id,$value)
	{
		$parameters = &$dom->getElementsByTagName('parameter');

		foreach ($parameters as $param)
		{
			if($id == $param->getAttribute("id"))
			{
				$param->setAttribute('value',$value);
				return true;
			}
		}

		return false;
	}
	
	private function createParameter($id, $value)
	{
		$element = new TXmlElement('parameter');
		$element->Attributes['id'] = $id;
		$element->Attributes['value'] = TPropertyValue::ensureString($value);
		return $element;
	}
}

?>